<?php
namespace app\common\model;
use think\Model;
use think\Validate;
use app\common\tool\Upload;
class LeasingRules extends Model
{
	protected $table = 'leasing_rules';
	protected $pk = 'leasing_rules_id';
	protected $autoWriteTimestamp = true;
	protected $createTime = 'leasing_rules_create_time';
	protected $updateTime = 'leasing_rules_update_time';

		// 验证规则
	protected $rule = [
		['leasing_rules_name|租赁模板规则名称', ['require']],
		['leasing_rules_content|租赁模板规则内容', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        'insert' => ['leasing_rules_name', 'leasing_rules_content'],
        'update' => ['leasing_rules_name', 'leasing_rules_content'],
    ];

    // 验证器
	public function intValidate(){
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }

    public static function refresh()
    {
        $merchant_id = session('merchant.merchant_id');
        // halt($merchant_id);
        $data = LeasingRules::field('leasing_rules_name,leasing_rules_id')->where("leasing_rules_merchant_id = $merchant_id and leasing_rules_is_delete = 0")->select();
        return $data;
    }  
    // 时间获取器
    // public function getLeasingRulesCreateTimeAttr($value)
    // {
    // 	$value = date('Y-m-d H:i:s', $value);
    // 	return $value;
    // }

    protected static function init()
	{
		LeasingRules::beforeInsert(function ($leasingRules) 
		{
			$merchant_id = session('merchant.merchant_id');
			$leasingRules['leasing_rules_merchant_id'] = $merchant_id;
		});
	}

	// 软删除
    public function changeDel($id)
    {
        $merchant_id = session('merchant.merchant_id');
        $result = leasingRules::where([
            'leasing_rules_id' => $id,
            'leasing_rules_merchant_id' => $merchant_id,
        ])->setField('leasing_rules_is_delete', 1); 
       if($result)
       {
            return true;
       }else
       {
            return false;
       }
    }

      // 运营后台的删除 
	public static function changeDel2($id)
	{
		$result = leasingRules::where('leasing_rules_id', $id)->setField('leasing_rules_is_delete', 1); 
	   if($result)
       {
            return true;
       }else
       {
            return false;
       }
    }

  
    

    public static function getLeaseRules($id)
    {
        $merchant_id = session('merchant')['merchant_id'];
        $details = leasingRules::where([
            'leasing_rules_id' => $id,
            'leasing_rules_merchant_id' => $merchant_id,
        ])->find();

        return $details;
    }


    public static function edit($id, $lease_name, $content)
    {
        $merchant_id = session('merchant')['merchant_id'];
    	$lease = new leasingRules;
    	$result = $lease->save([
			'leasing_rules_name' => $lease_name,
            'leasing_rules_content' => $content 
			],[
			'leasing_rules_id' => $id,
            'leasing_rules_merchant_id' => $merchant_id
            ]);

        if(!$result)
            return false;

        return true;
    }  
    // 租赁模板列表
    public static function showlist()
    {
        $merchant_id = session('merchant')['merchant_id'];
        $data = LeasingRules::field('leasing_rules_id,leasing_rules_name,leasing_rules_content,leasing_rules_create_time')
        ->where([
            'leasing_rules_merchant_id' => $merchant_id,
            'leasing_rules_is_delete' => 0
        ])->order('leasing_rules_id', 'desc')->paginate(2);
        
     	return $data;
    }


     // 运营平台租赁详情模板列表
    public static function totalLesringRules($pagesize)
    {
        $where['leasing_rules_is_delete'] = ['eq', 0];
        $keyword = input('param.keyword');
        if($keyword) 
        {
            $where['leasing_rules_name'] = ['like', "%$keyword%"];
        }

        $data = LeasingRules::field('leasing_rules_id,leasing_rules_name')
                ->order('leasing_rules_id', 'desc')
                ->where($where)
                ->paginate($pagesize);

       return  $data;
    } 


   
    
    // 
    public static function getLeasingrules($id) 
    {
        $where['leasing_rules_is_delete'] = ['eq', 0];
        $where['leasing_rules_id'] = ['eq', $id];
        $data = LeasingRules::field('leasing_rules_id,leasing_rules_name,leasing_rules_content')->where($where)->find();
        return $data;
    }

}